Completed
Push — master ( 06df20...fba648 )
by Sander
51s
created

$(document).ready   B

Complexity

Conditions 1
Paths 8

Size

Total Lines 168

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
c 4
b 0
f 0
nc 8
nop 0
dl 0
loc 168
rs 8.2857

9 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 8 1
A 0 10 1
A 0 9 1
A 0 14 2
A 0 6 1
A 0 14 1
A 0 12 2
B 0 76 1
A 0 6 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
$(document).ready(function () {
2
    function closeDoorhanger() {
3
        $('#password-doorhanger').slideUp(function () {
4
            API.runtime.sendMessage(API.runtime.id, {
5
                method: "passToParent",
6
                args: {'injectMethod': 'closeDoorhanger'}
7
            });
8
        });
9
    }
10
11
    function resizeIframe(height) {
12
        API.runtime.sendMessage(API.runtime.id, {
13
            method: "passToParent",
14
            args: {'injectMethod': 'resizeIframe', args: height}
15
        });
16
    }
17
18
    var default_account;
19
    var dh = $('#password-doorhanger');
20
    var btn_config = {
21
        'cancel': function () {
22
            return {
23
                text: API.i18n.getMessage('cancel'),
24
                onClickFn: function () {
25
                    closeDoorhanger();
26
                    API.runtime.sendMessage(API.runtime.id, {method: "clearMined"});
27
                }
28
            };
29
        },
30
        'save': function (data) {
31
            var save = API.i18n.getMessage('save');
32
            var update = API.i18n.getMessage('update');
33
            var btnText = (data.guid === null) ? save : update;
34
            return {
35
                text: btnText,
36
                onClickFn: function (account) {
37
                    API.runtime.sendMessage(API.runtime.id, {method: "saveMined", args: {account: account}});
38
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving_to', [account.vault.name]) + '...');
39
                    dh.find('.passman-btn').hide();
40
                },
41
                isCreate: (data.guid === null)
42
            };
43
        },
44
        'updateUrl': function (data) {
45
            return {
46
                text: 'Update',
47
                onClickFn: function () {
48
                    API.runtime.sendMessage(API.runtime.id, {method: "updateCredentialUrl", args: data});
49
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving'));
50
                    dh.find('.passman-btn').hide();
51
                }
52
            };
53
        },
54
        'ignore': function (data) {
55
            return {
56
                text: API.i18n.getMessage('ignore_site'),
57
                onClickFn: function () {
58
                    //closeToolbar();
59
                    API.runtime.sendMessage(API.runtime.id, {method: "ignoreSite", args: data.currentLocation});
60
                    dh.find('.toolbar-text').text(API.i18n.getMessage('site_ignored'));
61
                    dh.find('.passman-btn').hide();
62
                    setTimeout(function () {
63
                        closeDoorhanger();
64
                    }, 3000);
65
                }
66
            };
67
        }
68
    };
69
70
    API.runtime.sendMessage(API.runtime.id, {method: "getRuntimeSettings"}).then(function (settings) {
71
        var accounts = settings.accounts;
72
        default_account = accounts[0];
73
        API.runtime.sendMessage(API.runtime.id, {method: "getDoorhangerData"}).then(function (data) {
74
            if (!data) {
75
                return;
76
            }
77
            var buttons = data.buttons;
78
            data = data.data;
79
            var username = (data.username) ? data.username : data.email;
80
            var doorhanger_div = $('<div id="password-toolbar" style="display: none;">');
81
            $('<span>', {
82
                class: 'toolbar-text',
83
                text: data.title + ' ' + username + ' at ' + data.url
84
            }).appendTo(doorhanger_div);
85
86
87
            $.each(buttons, function (k, button) {
88
                var btn = button;
89
90
                button = btn_config[btn](data);
91
                var html_button;
92
93
                if (btn === 'save') {
94
                    var btn_text = API.i18n.getMessage('save_to', [default_account.vault.name]);
95
                    html_button = $('<button class="passman-btn passnman-btn-success btn-' + btn + '"></button>').text(btn_text);
96
                    if (button.isCreate && accounts.length > 1) {
97
                        var caret = $('<span class="fa fa-caret-down" style="margin-left: 5px; cursor: pointer;"></span>');
98
                        var menu = $('<div class="select_account" style="display: none;"></div>');
99
                        html_button.append(caret);
100
                        for (var i = 1; i < accounts.length; i++) {
101
                            var a = accounts[i];
102
                            var item = $('<div class="account"></div>').text(API.i18n.getMessage('save_to', [a.vault.name]));
103
                            /* jshint ignore:start */
104
                            (function (account, item) {
105
                                item.click(function (e) {
106
                                    e.stopPropagation();
107
                                    e.preventDefault();
108
                                    button.onClickFn(account);
109
                                });
110
                            })(a, item);
111
                            /* jshint ignore:end */
112
                            menu.append(item);
113
                        }
114
                        caret.click(function (e) {
115
                            e.stopPropagation();
116
                            e.preventDefault();
117
                            var isVisible = ($('.select_account').is(':visible'));
118
                            var height = (isVisible) ? 0 : accounts.length * 29;
119
                            if (!isVisible) {
120
                                resizeIframe(height);
121
                            }
122
                            menu.slideToggle(function () {
123
                                if(isVisible){
124
                                    resizeIframe(height);
125
                                }
126
                            });
127
                        });
128
                        caret.after(menu);
129
                    }
130
                    html_button.click(function () {
131
                        button.onClickFn(default_account);
132
                    });
133
                } else {
134
                    html_button = $('<button class="passman-btn passnman-btn-success"></button>').text(button.text);
135
                    html_button.click(function () {
136
                        button.onClickFn();
137
                    });
138
                }
139
140
                doorhanger_div.append(html_button);
141
            });
142
            dh.html(doorhanger_div);
143
            doorhanger_div.slideDown();
144
        });
145
    });
146
    var _this = {};
147
148
    function minedLoginSaved(args) {
149
        // If the login added by the user then this is true
150
151
        var saved = API.i18n.getMessage('credential_saved');
152
        var updated = API.i18n.getMessage('credential_updated');
153
        var action = (args.updated) ? updated : saved;
154
        $('#password-toolbar').html(action + '!');
155
        setTimeout(function () {
156
            closeDoorhanger();
157
        }, 2500);
158
159
    }
160
161
    _this.minedLoginSaved = minedLoginSaved;
162
    API.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
0 ignored issues
show
Unused Code introduced by
The parameter sendResponse is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
163
        //console.log('Method call', msg.method);
164
        if (_this[msg.method]) {
165
            _this[msg.method](msg.args, sender);
166
        }
167
    });
168
});